package com.bl.service.product.impl;

import java.util.List;

import com.bean.Product;
import com.bl.action.Command;
import com.bl.action.customer.BeanActions;
import com.bl.service.Service;
import com.bl.service.product.ProductFactory;
import com.bl.util.LoggingCommandExecutor;
import com.dao.factory.DaoFactory;
import com.dao.factory.DaoFactory.FactoryType;
import com.dao.factory.StartupFactoryType;

/**
 * @author ${user}
 */
public class ProductServiceWithLogging implements Service<Product> {

    FactoryType defaultFactoryType = new StartupFactoryType().getDefaultFactoryType();
    DaoFactory daoFactory = DaoFactory.getDaoFactory(defaultFactoryType);
    BeanActions<Product> productActions = new BeanActions<Product>(daoFactory.getProductDao(), new ProductFactory(defaultFactoryType));
    LoggingCommandExecutor commandExecutor = new LoggingCommandExecutor();

    @Override
    public void insertToDbOneByOne(final List<Product> aProducts) {
        final Command<Void> insertProductsToDbOneByOneAction = productActions.getInsertBeansToDbOneByOneAction(aProducts);
        commandExecutor.executeCommand(insertProductsToDbOneByOneAction, "Insert of the generated products performed in:");
    }


    @Override
    public void insertUsingBatches(final List<Product> aProducts) {
        final Command<Void> insertProductsUsingBatchesAction = productActions.getInsertBeansUsingBatchesAction(aProducts);
        commandExecutor.executeCommand(insertProductsUsingBatchesAction, "Batch Insert of the generated products performed in: ");
    }


    @Override
    public void deleteFromDbOneByOne(final List<Product> aProducts) {
        final Command<Void> deleteProductsFromDbOneByOneAction = productActions.getDeleteBeansFromDbOneByOneAction(aProducts);
        commandExecutor.executeCommand(deleteProductsFromDbOneByOneAction, "Delete * products from DB one by one: ");
    }


    @Override
    public void deleteUsingBatches(final List<Product> aProducts) {
        final Command<Void> deleteProductsUsingBatchesAction = productActions.getDeleteBeansUsingBatchesAction(aProducts);
        commandExecutor.executeCommand(deleteProductsUsingBatchesAction, "Batch Delete of the generated products: ");
    }


    @Override
    public Product findById(final Integer id) {
        throw new RuntimeException("Not implemented yet !");
    }


    @Override
    public List<Product> generateObjects(final int aNumberOfProducts) {
        final Command<List<Product>> generateProductsAction = productActions.getGenerateBeansAction(aNumberOfProducts);
        return commandExecutor.executeCommand(generateProductsAction, "generated " + aNumberOfProducts + " performed in: ");
    }
}
